using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using QLPMT_DTO;
using QLPMT_DAO;

namespace QLPMT_BUS
{
   public class ChiTietPhieuKhamBUS
    {
       public List<ChiTietPhieuKham> GetALL()
       {
           List<ChiTietPhieuKham> list = new List<ChiTietPhieuKham>();
           list = new ChiTietPhieuKhamDAO().GetAll();
           return list;
       }
       public string GetMaChiTietPhieuKham()
       {
           List<ChiTietPhieuKham> list = new List<ChiTietPhieuKham>();
           list = new ChiTietPhieuKhamDAO().GetAll();
           ChiTietPhieuKham temp = new ChiTietPhieuKham();
           foreach (ChiTietPhieuKham X in list)
           {
               temp.Machitiet = X.Machitiet;
           }
           string ma = "";
           if (temp.Machitiet != null)
           {
               char[] chuoi = temp.Machitiet.ToCharArray(2, temp.Machitiet.Length - 2);
               string chuoima = "";
               foreach (char charX in chuoi)
               {
                   chuoima = chuoima + charX;
               }
               int so = int.Parse(chuoima) + 1;
               ma = "CT" + so.ToString();
           }
           else
               ma = "CT1";
           return ma;

       }
       // Them chi tiet cho 1 phieu kham 
       public Boolean InsertDanhSachPhieuKHam(BindingList<ChiTietPhieuKham> list)
       {
           foreach (ChiTietPhieuKham X in list)
           {
               string mathuoc = X.Maloaithuoc;
               string madonvi = X.Madonvi;
               DonGiaThuoc dto = new DonGiaThuocBUS().GetDonGia(mathuoc, madonvi);
               dto.Soluong = dto.Soluong - X.Soluong;
               if (new DonGiaThuocBUS().UpDateSoLuongTon(dto) == true)
               {
                   int soluong = X.Soluong;
                   double dongia = new DonGiaThuocBUS().GetDonGia(X.Maloaithuoc, X.Madonvi).Dongia;
                   X.Thanhtien = dongia * soluong;
                   if (InsertChiTietPhieuKham(X) == false)
                   {
                       return false;
                   }
               }
               else
                   return false;
           }
           return true;
       }
       // Them chi tiet phieu kham moi
       public Boolean InsertChiTietPhieuKham( ChiTietPhieuKham dto)
       {
           try
           {
               new ChiTietPhieuKhamDAO().InsertChiTietPK(dto);
           }
           catch
           {
               return false;
           }
           return true;
       }
       // Tien thuoc cho 1 phieu kham
       public double GetTienThuoc(string maphieukham)
       {
           double tienthuoc = 0;
           List<ChiTietPhieuKham> list = new ChiTietPhieuKhamDAO().GetDanhSachChiTietPhieuKham(maphieukham);
           foreach (ChiTietPhieuKham X in list)
           {
               tienthuoc += X.Thanhtien;
           }
           return tienthuoc;
       }
       // danh sach chi tiet phieu kham 
       public List<ChiTietPhieuKham> GetDanhSachChiTietPhieuKham(string maphieukham)
       {
           return new ChiTietPhieuKhamDAO().GetDanhSachChiTietPhieuKham(maphieukham);
       }
       // Kiem tra ma cach dug
       public Boolean IsCachDung(CachDung dto)
       {
           int count = new ChiTietPhieuKhamDAO().IscachDung(dto).Count;
           if (count <= 0)
               return false;
           return true;
       }
    }
}
